package com.senven.settled.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.senven.common.entity.Branch;
import com.senven.common.entity.Settledlibrary;
import com.senven.common.entity.User;
import com.senven.common.vo.CommonResult;
import com.senven.settled.config.Factory;
import com.senven.settled.feign.BranchFeign;
import com.senven.settled.feign.UsersFeign;
import com.senven.settled.mapper.SettledlibraryMapper;
import com.senven.settled.service.ISettledlibraryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senven.settled.vo.PhoneLetter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wt
 * @since 2022-09-05
 */
@Service
public class SettledlibraryServiceImpl extends ServiceImpl<SettledlibraryMapper, Settledlibrary> implements ISettledlibraryService {

    @Autowired
    private SettledlibraryMapper settledlibraryMapper;

    @Autowired
    private UsersFeign usersFeign;

    @Autowired
    private BranchFeign branchFeign;

    @Override
    public CommonResult insertSettled(Settledlibrary settledlibrary) {
        int i = settledlibraryMapper.insert(settledlibrary);
        if (i>0){
            return new CommonResult(2000,"添加成功",null);
        }
        return new CommonResult(5000,"添加失败",null);
    }

    //查询所有商家入驻审核信息
    @Override
    public CommonResult findAllSettled(Integer currentPage, Integer pageSize, Map map) {
        System.out.println(map);
        PageHelper.startPage(currentPage,pageSize);
        List<Settledlibrary> list = settledlibraryMapper.selectListSettled(map);
        if (list!=null){
            PageInfo pageInfo = new PageInfo(list);
            return new CommonResult(2000,"查询成功",pageInfo);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    //查询状态为待审核的商家审核信息
    @Override
    public CommonResult findAllSettle(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Settledlibrary> list = settledlibraryMapper.selectListSettle();
        if (list!=null){
            PageInfo pageInfo = new PageInfo(list);
            return new CommonResult(2000,"查询成功",pageInfo);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    //商家入驻审核通过,2改变审核状态settled,，3存储商家信息user，4给商家角色role，5将入驻的商家添加到店铺表，6给商家发送短信
    @Transactional
    @Override
    public CommonResult insertUserByRoleBySettled(Settledlibrary settledlibrary) {
        System.out.println(settledlibrary);
        settledlibrary.setExamstate(2);
        LocalDateTime now = LocalDateTime.now();
        //2改变审核状态settled
        int i = settledlibraryMapper.updateById(settledlibrary);
        //随机生成账号、密码、和盐、并将大写字母转为小写
        String username = Factory.getRandomCharacters(8).toLowerCase(Locale.ROOT);
        String password = Factory.getRandomCharacters(8).toLowerCase(Locale.ROOT);
        String salt = Factory.getRandomCharacters(3).toLowerCase(Locale.ROOT);
        System.out.println(username);
        System.out.println(password);
        //拼接字符串--账号和密码--通过短信发送给客户
//        StringBuffer str = new StringBuffer("code:");
//        str.append(username+",expire_at:");
//        str.append(password);
//        System.out.println(str);
//        String s1 = String.valueOf(str);

        StringBuffer str = new StringBuffer("code:");
        str.append("审核通过,expire_at:");
        str.append(5);
        System.out.println(str);
        String s1 = String.valueOf(str);


        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setTel(settledlibrary.getShoptel());
        user.setAddress(settledlibrary.getAddress());
        user.setUrl(settledlibrary.getShopurl());
        user.setSalt(salt);
        user.setRealname(settledlibrary.getShopname());
        System.out.println(user);
        //3调用user接口添加新商户--4.给商户添加角色
        String s = usersFeign.insertShop(user);
        //5.添加店铺
        Branch branch = new Branch();
        branch.setBranchname(settledlibrary.getBranchname());
        branch.setAddress(settledlibrary.getAddress());
        branch.setTel(settledlibrary.getShoptel());
        branch.setDescrible(settledlibrary.getBranchdescrible());
        branch.setBranchUrl(settledlibrary.getShopurl());
        branch.setUserid(s);
        branch.setStatus("1");
        int i1 = branchFeign.insertBranch(branch);
        System.out.println("============="+i1);

        //6.通过短信给商家发账号密码
        String shoptel = settledlibrary.getShoptel();
        System.out.println(shoptel);
        PhoneLetter.duanxin(s1,shoptel);
        System.out.println(username);
        System.out.println(password);
        if (i>0){
            return new CommonResult(2000,"审核通过",null);
        }
        return new CommonResult(5000,"审核失败",null);

    }

    //商家入驻审核驳回
    @Override
    public CommonResult updateSettledState(Settledlibrary settledlibrary) {
        int i = settledlibraryMapper.updateById(settledlibrary);
        if (i>0){
            //调用方法给商家发失败短信
//            String str = "code:入驻失败,expire_at:请重新提交";
            String str = "code:入驻失败,expire_at:5";
            String shoptel = settledlibrary.getShoptel();
            PhoneLetter.duanxin(str,shoptel);
            return  new CommonResult(2000,"驳回成功",null);
        }
        return new CommonResult(5000,"驳回失败",null);
    }
}
